<!-- 鱼塘管理 -->
<template>
  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" @search="search">
      </SearchForm>
    </template>
    <template v-slot:table>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border height="100%"
        :row-key="row => {
        return row.id;
      }
        " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column type="selection" width="55" :reserve-selection="true"
          :selectable="(row) => { return !row.selectable }" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.id ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column prop="feedMachineName" label="投料机名称" width="200" />
        <el-table-column prop="deviceId" label="投料及编号" width="230" />
        <el-table-column prop="fishPondName" label="鱼塘名称" />
        <el-table-column prop="feedStartTime" label="投料开始时间" width="200">
          <template v-if="scoped.$index + 1" slot-scope="scoped">{{ formatDateTimeMHS(scoped.row.feedStartTime)
            }}</template>
        </el-table-column>
        <el-table-column prop="feedEndTime" label="投料结束时间" width="200">
          <template v-if="scoped.$index + 1" slot-scope="scoped">{{ formatDateTimeMHS(scoped.row.feedEndTime)
            }}</template>
        </el-table-column>
        <el-table-column prop="materialName" label="物料名称" width="150" />
        <el-table-column prop="beforeWeight" label="投喂前重量">
          <template v-if="scoped.$index + 1 && scoped.row.beforeWeight" slot-scope="scoped">{{
        (Number(scoped.row.beforeWeight) / 2).toFixed(2) || '' }}斤</template>
        </el-table-column>
        <el-table-column prop="afterWeight" label="投喂后重量">
          <template v-if="scoped.$index + 1 && scoped.row.afterWeight" slot-scope="scoped">{{
        (Number(scoped.row.afterWeight) / 2).toFixed(2) || '' }}斤</template>
        </el-table-column>
        <el-table-column prop="targetWeight" label="本次投喂目标量">
          <template v-if="scoped.$index + 1 && scoped.row.targetWeight" slot-scope="scoped">{{
        (Number(scoped.row.targetWeight) / 2).toFixed(2) || '' }}斤</template>
        </el-table-column>
        <el-table-column prop="actualWeight" label="本次实际投喂量">
          <template v-if="scoped.$index + 1 && scoped.row.actualWeight" slot-scope="scoped">{{
        (Number(scoped.row.actualWeight) / 2).toFixed(2) || '' }}斤</template>
        </el-table-column>
      </el-table>
    </template>
    <template v-slot:pagination>
      <Pagination @current-change="changeCurrentPage" :size="pageSize" :page="currentPage" :total="totalSize">
      </Pagination>
    </template>
  </MainSlot>
</template>

<script>
import MainSlot from "@/components-production-management/MainSlot";
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components-production-management/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new2.vue";
import SearchForm from "@/components-production-management/SearchForm";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import feedMachineWorkApi from "@/api/feedMachineWork";
export default {
  name: "FishPondManage",
  components: { ActionBar, Pagination, BaseMainSlot, MainSlot, SearchForm },
  mixins: [mymixin],
  data() {
    return {
      // 查询条件：鱼塘名称
      pondName: "",
      pageSize: 30,
      checkedIdList: [],
      formItemList: [
        {
          label: "鱼塘名称",
          type: "input",
          param: "pondName",
          multiple: false,
          isShow: true,
        }]
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    this.queryPageList();
  },
  mounted() { },
  methods: {
    search(data) {
      console.log(data)
      this.page = 1;
      this.pondName = data?.pondName || ''
      this.queryPageList();
    },
    searchPage() {
      this.currentPage = 1
      this.queryPageList();
      setTimeout(() => {
        this.queryPageList();
      }, 200);
    },
    queryPageList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          fishPondName: this.pondName || ""
        }
      };
      feedMachineWorkApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.queryPageList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll(list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.id) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.id) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.id) {
          return item.id;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",

      })
        .then(() => {
          FishPondApi.delById({ id: data.id }).then(res => {
            if (res.success === true) {
              this.$message.success("删除成功!");
              this.queryPageList();
              this.resetActionBar();
            }
          });
        })
        .catch(() => {
          this.resetActionBar();
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 表格删除
    delmessage() {
      FishPondApi.delListById({ idList: this.checkedIdList }).then(res => {
        if (res.success) {
          this.$message.success("删除成功");
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.id) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .module {}
</style>